import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:app_core/app_core.dart';
import 'package:yh_core_flutter_project/project/common/constant/colors.dart';
import 'app_base_logic.dart';
import '../widgets/easy_refresh_widget.dart';

/// 基础页面基类 - 提供统一的UI结构和工具方法
abstract class AppBasePage<T extends AppBaseLogic> extends StatelessWidget {
  const AppBasePage({super.key});

  ///子类必须实现
  // T get logic;

  ///构建页面内容
  Widget buildContent(BuildContext context);

  /// 页面标题
  String? get title => null;

  /// 是否显示AppBar
  bool get showAppBar => true;

  /// 是否显示返回按钮
  bool get showBack => true;

  /// 右侧按钮
  /// List<Widget>? actions
  List<Widget>? get actions => null;

  /// 背景颜色
  Color? get backgroundColor => AppColor.scaffoldBackground;

  /// 构建自定义AppBar
  PreferredSizeWidget? buildAppBar(BuildContext context) => null;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: showAppBar ? _getAppBar(context) : null,
      backgroundColor: backgroundColor,
      body: buildContent(context),
    );
  }

  /// 构建导航栏
  PreferredSizeWidget? _getAppBar(BuildContext context) {
    final customAppBar = buildAppBar(context);
    if (customAppBar != null) return customAppBar;
    return MyAppBar(
      title ?? '',
      showBack: showBack,
      backgroundColor: backgroundColor,
      actions:actions,
    );
  }

  /// 包装刷新组件
  Widget wrapWithRefresh({
    required Widget child,
    Future<void> Function()? onRefresh,
    Future<void> Function()? onLoadMore,
  }) {
    return EasyRefreshWidget(
      enablePullToRefresh: onRefresh != null,
      enableLoadMore: onLoadMore != null,
      onRefresh: onRefresh,
      onLoadMore: onLoadMore,
      headerConfig: null,
      footerConfig: null,
      child: child,
    );
  }
}
